
	import java.util.InvalidPropertiesFormatException;
import java.util.*;
import java.io.*;

import javax.xml.parsers.*;

import org.w3c.dom.*;
import org.xml.sax.SAXException;
	public class XML
	{
		Scanner input=new Scanner(System.in);
		Properties prop = new Properties();
		private NodeList nodeList;
		public void NewXML() throws FileNotFoundException, IOException{
		// ghi vao xml
			System.out.println("Nhap ten file dau ra: ");
			String filename=input.nextLine();
			System.out.println("Ban muon nhap vao bao nhieu phan tu? ");
			int n=input.nextInt();
			String a,b;
			System.out.println("Moi nhap gia tri cac node:");
			for(int i=0;i<n;i++){
				System.out.println("Gia tri node "+i+": ");
				a=input.nextLine();
				b=input.nextLine();
				prop.setProperty(a,b);
				System.out.println("\n");
			}
			prop.storeToXML(new FileOutputStream(filename),"\nThoi gian ghi file: "+new Date().toString()+" By JGuru");
		}
		public void ReadXML() throws InvalidPropertiesFormatException, FileNotFoundException, IOException{
			        try {
			            Scanner input=new Scanner(System.in);
			            System.out.println("Nhap vao file can doc: ");
			            String fileName=input.nextLine();
			            File xmlFile = new File(fileName);
			            DocumentBuilder dBuilder;
			            dBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			            Document doc = dBuilder.parse(xmlFile);
			            System.out.println("Root element : " + doc.getDocumentElement().getNodeName());
			            //Doc va in ra gia tri cua cac node
			            if (doc.hasChildNodes()) {
			                doc.getChildNodes();
			            }
			            System.out.println("End Root element: " + doc.getDocumentElement().getNodeName());
			        } catch (ParserConfigurationException | SAXException | IOException e) {
			        }
			        for (int count = 0; count < nodeList.getLength(); count++) {
			            Node tempNode = nodeList.item(count);
			            if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
			                /*
			                 * Ten cua Node mo tab tempNode.getNodeName() + " [OPEN]");
			                 * Gia tri cua node mo tab "Node Value = " + tempNode.getTextContent());
			                */
			                tempNode.getNodeName();
			                System.out.print(tempNode.getTextContent());
			                if (tempNode.hasAttributes()) {
			                    NamedNodeMap nodeMap = tempNode.getAttributes();
			                    for (int i = 0; i < nodeMap.getLength(); i++) {
			                        /*In ra ten cua attributes: node.getNodeName()
			                         * In ra gia tri cua attributes: node.getNodeValue()
			                         */
			                        Node node = nodeMap.item(i);
			                        node.getNodeName();
			                        System.out.print(node.getNodeValue());
			                    }
			                }
			                    /*
			                     * lap lai lan nua neu nhu tim thay node con
			                     * if (tempNode.hasChildNodes()) {
			                     * De quy node con printNote(tempNode.getChildNodes());
			                     * In ra ten cua nut tab dong: tempNode.getNodeName()
			                     * }
			                     */
			                tempNode.getNodeName();//Lay ten cua node dong tab
			            }
			        }
			    }
	public static void main(String args[]) throws Exception
	{
		XML execute=new XML();
		execute.NewXML();
		System.out.println("************************");
		execute.ReadXML();
  }
}
